/*****************************************************************************
*        描述 :
*             文件描述
*        CLR版本:            3.5
*        创建者 :            蒋 光
*        个人域名:           jiangguang.net.cn
*        命名空间名称:       ECP.DataAccess
*        文件名:             BaseFolderDao.cs
*        创建系统时间:       2012/11/2 19:06:59
*        创建年份:           2012
*
/*****************************************************************************/

namespace ECP.DataAccess
{
    using System.Data;
    using ECP.DataAccess.Utilities;
    using ECP.Model;
    using ECP.Utilities;

    public class BaseFolderDao : BaseDao
    {
        public BaseFolderDao()
        {
            base.CurrentTableName = BaseFolderTable.TableName;
        }

        public BaseFolderDao(IDbHelper dbHelper)
            : this()
        {
            base.DbHelper = dbHelper;
        }

        public BaseFolderDao(BaseUserInfo userInfo)
            : this()
        {
            base.UserInfo = userInfo;
        }

        public BaseFolderDao(IDbHelper dbHelper, BaseUserInfo userInfo)
            : this(dbHelper)
        {
            base.UserInfo = userInfo;
        }

        public string Add(BaseFolderEntity folderEntity, out string statusCode)
        {
            string str = string.Empty;
            if (this.Exists(BaseFolderTable.FieldParentID, folderEntity.ParentID, BaseFolderTable.FieldFolderName, folderEntity.FolderName))
            {
                statusCode = StatusCode.ErrorFullNameExist.ToString();
                return str;
            }
            str = this.AddEntity(folderEntity);
            statusCode = StatusCode.OKAdd.ToString();
            return str;
        }

        public string AddEntity(BaseFolderEntity folderEntity)
        {
            string sequence = BaseSequenceDao.Instance.GetSequence(base.DbHelper, BaseFolderTable.TableName);
            if (string.IsNullOrEmpty(folderEntity.ID))
            {
                folderEntity.ID = BaseSequenceDao.Instance.NewGuid();
            }
            folderEntity.SortCode = sequence;
            SQLBuilder sqlBuilder = new SQLBuilder(base.DbHelper);
            sqlBuilder.BeginInsert(BaseFolderTable.TableName);
            sqlBuilder.SetValue(BaseFolderTable.FieldID, folderEntity.ID);
            this.SetEntity(sqlBuilder, folderEntity);
            sqlBuilder.SetValue(BaseFolderTable.FieldCreateUserID, base.UserInfo.ID);
            sqlBuilder.SetValue(BaseFolderTable.FieldCreateUserRealname, base.UserInfo.Realname);
            sqlBuilder.SetDBNow(BaseFolderTable.FieldCreateDate);
            sqlBuilder.SetValue(BaseFolderTable.FieldModifyUserID, base.UserInfo.ID);
            sqlBuilder.SetValue(BaseFolderTable.FieldModifyUserRealname, base.UserInfo.Realname);
            sqlBuilder.SetDBNow(BaseFolderTable.FieldModifyDate);
            if (sqlBuilder.EndInsert() <= 0)
            {
                return string.Empty;
            }
            return sequence;
        }

        public override int BatchSave(DataTable dataTable)
        {
            int num = 0;
            BaseFolderEntity folderEntity = new BaseFolderEntity();
            foreach (DataRow row in dataTable.Rows)
            {
                if (row.RowState == DataRowState.Deleted)
                {
                    string id = row[BaseFolderTable.FieldID, DataRowVersion.Original].ToString();
                    if (id.Length > 0)
                    {
                        num += this.DeleteEntity(id);
                    }
                }
                if ((row.RowState == DataRowState.Modified) && (row[BaseFolderTable.FieldID, DataRowVersion.Original].ToString().Length > 0))
                {
                    folderEntity.GetFrom(row);
                    num += this.UpdateEntity(folderEntity);
                }
                if (row.RowState == DataRowState.Added)
                {
                    this.GetFrom(row);
                    num += (this.AddEntity(folderEntity).Length > 0) ? 1 : 0;
                }
                if (row.RowState != DataRowState.Unchanged)
                {
                    DataRowState rowState = row.RowState;
                }
            }
            return num;
        }

        public int MoveTo(string id, string parentID)
        {
            return this.SetProperty(id, BaseOrganizeTable.FieldParentID, parentID);
        }

        public DataTable Search(string search)
        {
            string sqlQuery = string.Empty;
            sqlQuery = " SELECT *  FROM " + BaseFolderTable.TableName + " WHERE " + BaseFolderTable.FieldFolderName + " LIKE " + base.DbHelper.GetParameter(BaseFolderTable.FieldFolderName) + " OR " + BaseFolderTable.FieldDescription + " LIKE " + base.DbHelper.GetParameter(BaseFolderTable.FieldDescription);
            DataTable dataTable = new DataTable(BaseFolderTable.TableName);
            search = search.Trim();
            if (search.IndexOf("%") < 0)
            {
                search = "%" + search + "%";
            }
            base.DbHelper.AttachAdapterParameters(base.DbHelper.MakeParameters(BaseFolderTable.FieldFolderName, search));
            base.DbHelper.AttachAdapterParameters(base.DbHelper.MakeParameters(BaseFolderTable.FieldDescription, search));
            base.DbHelper.Fill(dataTable, sqlQuery);
            return dataTable;
        }

        private void SetEntity(SQLBuilder sqlBuilder, BaseFolderEntity folderEntity)
        {
            sqlBuilder.SetValue(BaseFolderTable.FieldParentID, folderEntity.ParentID);
            sqlBuilder.SetValue(BaseFolderTable.FieldFolderName, folderEntity.FolderName);
            sqlBuilder.SetValue(BaseFolderTable.FieldSortCode, folderEntity.SortCode);
            sqlBuilder.SetValue(BaseFolderTable.FieldDescription, folderEntity.Description);
            sqlBuilder.SetValue(BaseFolderTable.FieldEnabled, folderEntity.Enabled ? 1 : 0);
        }

        public int Update(BaseFolderEntity folderEntity, out string statusCode)
        {
            int num = 0;
            if (DbLogic.IsModifed(base.DbHelper, BaseFolderTable.TableName, folderEntity.ID, folderEntity.ModifyUserID, folderEntity.ModifyDate))
            {
                statusCode = StatusCode.ErrorChanged.ToString();
                return num;
            }
            if (this.Exists(BaseFolderTable.FieldParentID, folderEntity.ParentID, BaseFolderTable.FieldFolderName, folderEntity.FolderName, folderEntity.ID))
            {
                statusCode = StatusCode.ErrorFullNameExist.ToString();
                return num;
            }
            num = this.UpdateEntity(folderEntity);
            if (num == 1)
            {
                statusCode = StatusCode.OKUpdate.ToString();
                return num;
            }
            statusCode = StatusCode.ErrorDeleted.ToString();
            return num;
        }

        public int UpdateEntity(BaseFolderEntity folderEntity)
        {
            SQLBuilder sqlBuilder = new SQLBuilder(base.DbHelper);
            sqlBuilder.BeginUpdate(BaseFolderTable.TableName);
            this.SetEntity(sqlBuilder, folderEntity);
            sqlBuilder.SetValue(BaseFolderTable.FieldModifyUserID, base.UserInfo.ID);
            sqlBuilder.SetValue(BaseFolderTable.FieldModifyUserRealname, base.UserInfo.Realname);
            sqlBuilder.SetDBNow(BaseFolderTable.FieldModifyDate);
            sqlBuilder.SetWhere(BaseFolderTable.FieldID, folderEntity.ID);
            return sqlBuilder.EndUpdate();
        }
    }
}